package com.xpgk.mvc.ddc.bgdj.ywsl;


import java.text.SimpleDateFormat;
import java.util.Date;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.ddc.ddc_commomUtil.PictureService;
import com.xpgk.mvc.ddc.entity.Business;
import com.xpgk.mvc.ddc.entity.Credentials;
import com.xpgk.mvc.ddc.entity.Owner;
import com.xpgk.mvc.ddc.entity.Plate;
import com.xpgk.mvc.ddc.entity.Vehicle;
import com.xpgk.mvc.ddc.entity.VehicleParemeters;
import com.xpgk.mvc.ddc.statistic.DailyStasticService;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.user.User;
import com.xpgk.util.SerialNumberUtil;

public class BgdjYwslController  extends  BaseController{
	private DailyStasticService dailyStasticService;
	private PictureService pictureService = new PictureService();
	
	public void index() {
		if(!getCUserOrganIds().equals("f73bfba518ff4048afbdc006e3eda906")){
			setAttr("orgid", getCUserOrganIds());
		}
		render("ywsllist.html");
	}
	
	public  void  getlist(){
		String json = paging(splitPage, Vehicle.sqlId_splitPageSelect,Vehicle.sqlId_splitPageFrom2);
		renderJson(json);
	}
	
	public  void  details(){
		ids=getPara("ids");
		Vehicle vehicle = Vehicle.dao.findById(ids);
		setAttr("owner", Owner.dao.findById(vehicle.get("OWNERIDS")));
		setAttr("vehicleparemeters", VehicleParemeters.dao.findById(vehicle.get("VEHICLEPAREMETERSIDS")));
		setAttr("plate", Plate.dao.findById(vehicle.get("PLATEIDS")));
		render("details.html");
	}
	
	public  void  ywsl(){
		Organization  org =  Organization.dao.findById(getCUserOrganIds());
		setAttr("LSH",SerialNumberUtil.getSerialNumber(org, "03"));
		ids=getPara("ids");
		Vehicle vehicle = Vehicle.dao.findById(ids);
		setAttr("vehicle", vehicle);
		setAttr("owner", Owner.dao.findById(vehicle.get("OWNERIDS")));
		setAttr("vehicleparemeters", VehicleParemeters.dao.findById(vehicle.get("VEHICLEPAREMETERSIDS")));
		setAttr("plate", Plate.dao.findById(vehicle.get("PLATEIDS")));
		render("ywsl.html");
	}

	@Before(Tx.class)
	@SuppressWarnings("static-access")
	public  void  saveInfo(){	
		RenderBean result = new RenderBean();
		
		try {
			User user = getCUser();
			String jbrids = user.getStr(User.column_ids);
			String jbr = user.getStr(User.column_name);
			String organids = user.getStr(User.column_organizationids);
			Organization organization = Organization.dao.findById(organids);
			String organ = organization.getStr(Organization.column_name);
			
			SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			Business  business = getModel(Business.class,"business");
			Vehicle vehicle = Vehicle.dao.findById(business.get("VEHICLEIDS"));
			VehicleParemeters  vehicleparemeters2 = VehicleParemeters.dao.findById(vehicle.get("VEHICLEPAREMETERSIDS"));
			VehicleParemeters  vehicleparemeters = getModel(VehicleParemeters.class,"vehicleparemeters");
			Owner owner = Owner.dao.findById(vehicle.get("OWNERIDS"));
			Credentials credentials2 = Credentials.dao.findById(vehicle.get("CREDENTIALSIDS"));
			Plate  plate = getModel(Plate.class,"plate");
//		plate.set(Plate.column_HPHM, "晋B"+plate.get("HPHM"));
			
			vehicle.set(vehicle.column_CLLX, vehicleparemeters2.get("CLLX"));
			vehicle.set(vehicle.column_CCRQ, vehicleparemeters2.get("CCRQ"));
			vehicle.set(vehicle.column_SYNX, vehicleparemeters2.get("SYNX"));
			vehicle.set(vehicle.column_HPHM, plate.get("HPHM"));
			vehicle.set(vehicle.column_OWNERNAME, owner.get("OWNERNAME"));
			vehicle.set(vehicle.column_BUSINESSTYPE, "3");
			vehicle.update();
			
			vehicleparemeters.set(vehicleparemeters.column_VEHICLEIDS, vehicle.get("IDS"));
			vehicleparemeters.set(vehicleparemeters.column_HPHM, plate.get("HPHM"));
			vehicleparemeters.set(vehicleparemeters.column_CLLX, vehicleparemeters2.get("CLLX"));
			vehicleparemeters.set(vehicleparemeters.column_SYXZ, vehicleparemeters2.get("SYXZ"));
			vehicleparemeters.set(vehicleparemeters.column_CSYS, vehicleparemeters2.get("CSYS"));
			vehicleparemeters.save();
			
			plate.set(plate.column_VEHICLEIDS, vehicle.get("IDS"));
			plate.set(plate.column_STATUS, "1");
			plate.set(plate.column_CREATEDATE, sdf.format(new Date()));
			plate.set(plate.column_JBRIDS, jbrids);
			plate.set(plate.column_ORGANIDS, organids);
			plate.save();
			
			Credentials credentials = new Credentials();
			credentials.set(credentials.column_HPHM, plate.get("HPHM"));
			credentials.set(credentials.column_DABH, business.get("LSH"));
			credentials.set(credentials.column_VEHICLEIDS, vehicle.get("IDS"));
			credentials.set(credentials.column_CLLX, vehicleparemeters.get("CLLX"));
			credentials.set(credentials.column_OWNERNAME, owner.get("OWNERNAME"));
//		credentials.set(credentials.column_COMPANYNAME, owner.get("COMPANYNAME"));
			credentials.set(credentials.column_ADDRESS, owner.get("ADDRESS"));
			credentials.set(credentials.column_DETAILADDRESS, owner.get("DETAILADDRESS"));
			credentials.set(credentials.column_SYXZ, vehicleparemeters.get("SYXZ"));
			credentials.set(credentials.column_CLPP, vehicleparemeters.get("CLPP"));
			credentials.set(credentials.column_CLXH, vehicleparemeters.get("CLXH"));
			credentials.set(credentials.column_ZCZL, vehicleparemeters.get("ZCZL"));
			credentials.set(credentials.column_HDZRS, vehicleparemeters.get("HDZRS"));
			credentials.set(credentials.column_STATUS,"1");
			credentials.set(credentials.column_CREATEDATE, sdf.format(new Date()));
			credentials.set(credentials.column_ORGANIDS,organids);
			credentials.set(credentials.column_ORGAN, organ);
			credentials.set(credentials.column_JBRIDS, jbrids);
			credentials.set(credentials.column_ORGAN, jbr);
			credentials.save();
			
			business.set(business.column_TYPE, "3");
			business.set(business.column_VEHICLEIDS, vehicle.get("IDS"));
			business.set(business.column_OLDCREDENTIALSIDS, credentials2.get("IDS"));
			business.set(business.column_NEWCREDENTIALSIDS, credentials.get("IDS"));
			business.set(business.column_STATUS, "1");
			business.set(business.column_CLLX, vehicleparemeters.get("CLLX"));
			business.set(business.column_NEWOWNERIDS, owner.get("IDS"));
			business.set(business.column_NEWOWNERNAME, owner.get("OWNERNAME"));
//		business.set(business.column_NEWCOMPANYNAME, owner.get("COMPANYNAME"));
			business.set(business.column_OLDPAREMETERSIDS, vehicleparemeters2.get("IDS"));
			business.set(business.column_NEWPAREMETERSIDS, vehicleparemeters.get("IDS"));
			business.set(business.column_CLLX, vehicleparemeters.get("CLLX"));
//		business.set(business.column_CLPP, vehicleparemeters.get("CLPP"));
			business.set(business.column_NEWPLATEIDS, plate.get("IDS"));
			business.set(business.column_NEWHPHM, plate.get("HPHM"));
			business.set(business.column_ORGANIDS, organids);
			business.set(business.column_ORGANNAME, organ);
			business.set(business.column_YWSLDATE, sdf.format(new Date()));
			business.set(business.column_YWSLJBRIDS, jbrids);
			business.set(business.column_YWSLJBR, jbr);
			business.save();
			
			Vehicle.dao.findById(vehicle.get("IDS")).set(Vehicle.column_VEHICLEPAREMETERSIDS, vehicleparemeters.get("IDS")).update();
			VehicleParemeters.dao.findById(vehicleparemeters.get("IDS")).set(VehicleParemeters.column_PLATEIDS, plate.get("IDS")).update();
			
			dailyStasticService.saveDailyStastic(organids,organ,sdf.format(new Date()),"3","1");
			
			String picStr1 = getPara("picStr1");
			String picStr2 = getPara("picStr2");
			String picStr3 = getPara("picStr3");
			String picStr4 = getPara("picStr4");
			String BUSINESSIDS = business.get("IDS");
			String VEHICLEIDS = vehicle.get("IDS");
			if(picStr1.length()>0){
				pictureService.uploadPhoto(picStr1,BUSINESSIDS,VEHICLEIDS,"PICTURE1","PICTURE1");
			}
			if(picStr2.length()>0){
				pictureService.uploadPhoto(picStr2,BUSINESSIDS,VEHICLEIDS,"PICTURE2","PICTURE2");
			}
			if(picStr3.length()>0){
				pictureService.uploadPhoto(picStr3,BUSINESSIDS,VEHICLEIDS,"PICTURE3","PICTURE3");
			}
			if(picStr4.length()>0){
				pictureService.uploadPhoto(picStr4,BUSINESSIDS,VEHICLEIDS,"PICTURE4","PICTURE4");
			}
			result.setCode(ConstantRender.render_success_code);
			result.setMsg("保存成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setCode(ConstantRender.render_error_code);
			result.setMsg("保存失败");
		}
		renderJson(result);
	}	
}
